package zpin.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import util.dao.Pagination;
import zpin.dao.AboutUsDao;
import zpin.dao.CompanyDao;
import zpin.dao.PersonDao;
import zpin.model.AboutUs;
import zpin.model.Company;
import zpin.model.Person;

import com.opensymphony.xwork2.ActionSupport;

public class AboutUsAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long id;

	private AboutUs aboutUs;

	private Person person;

	private Company company;

	private AboutUsDao aboutUsDao;

	private PersonDao personDao;

	private CompanyDao companyDao;

	private Pagination<AboutUs> pagination = new Pagination<AboutUs>();

	List<AboutUs> aboutuslist = new ArrayList<AboutUs>();

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public Company getCompany() {
		return company;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Pagination<AboutUs> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<AboutUs> pagination) {
		this.pagination = pagination;
	}

	public List<AboutUs> getAboutuslist() {
		return aboutuslist;
	}

	public void setAboutuslist(List<AboutUs> aboutuslist) {
		this.aboutuslist = aboutuslist;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public AboutUs getAboutUs() {
		return aboutUs;
	}

	public void setAboutUs(AboutUs aboutUs) {
		this.aboutUs = aboutUs;
	}

	public AboutUsDao getAboutUsDao() {
		return aboutUsDao;
	}

	public void setAboutUsDao(AboutUsDao aboutUsDao) {
		this.aboutUsDao = aboutUsDao;
	}

	public String create() {
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Calendar calendar = Calendar.getInstance();
		String tempdate = dateFormat.format(calendar.getTime());
		aboutUs.setDate(tempdate);
		if (Person.class.isInstance(ServletActionContext.getRequest()
				.getSession().getAttribute("authorization"))) {
			person = (Person) ServletActionContext.getRequest().getSession()
					.getAttribute("authorization");
			aboutUs.setPerson(person);
		}
		if (Company.class.isInstance(ServletActionContext.getRequest()
				.getSession().getAttribute("authorization"))) {
			company = (Company) ServletActionContext.getRequest().getSession()
					.getAttribute("authorization");
			aboutUs.setCompany(company);
		}
		aboutUsDao.create(aboutUs);
		return SUCCESS;
	}

	public String delete() {
		return SUCCESS;
	}

	public String update() {
		return SUCCESS;
	}

	public String select() {
		if (pagination == null) {
			this.pagination = new Pagination<AboutUs>();
		}
		if (pagination.getSize() == 0) {
			this.pagination.setSize(3);
		}
		if (pagination.getNo() == 0) {
			this.pagination.setNo(1);
		}
		aboutUsDao.browse(pagination);
		aboutuslist = pagination.getResults();
		return SUCCESS;
	}

	public String leaveMessage() {
		if (Person.class.isInstance(ServletActionContext.getRequest()
				.getSession().getAttribute("authorization"))) {
			Person temp = (Person) ServletActionContext.getRequest()
					.getSession().getAttribute("authorization");
			this.person = personDao.findById(temp.getId());
		}
		if (Company.class.isInstance(ServletActionContext.getRequest()
				.getSession().getAttribute("authorization"))) {
			Company temp = (Company) ServletActionContext.getRequest()
					.getSession().getAttribute("authorization");
			this.company = companyDao.findById(temp.getId());
		}
		return SUCCESS;
	}

	public String input() {
		return "input";
	}
}
